###### functions begin ######

udp_server() {
    for orbital in {11..12}; do
        for id in {01..11}; do
            node="s${orbital}${id}"
            ip="192.168.${orbital}.${id#0}"     # 去掉末尾的0
            port="1${orbital}${id}"
            docker exec ${node} bash -c "server --ip=${ip} --port=${port} > ~/logfile/server/${node}.log 2>&1 &"
        done
    done
    # kill -9 $(ps ux | grep -i '\bserver --ip' | awk '{print $2}')
}

udp_client() {
    bandwidth=10   #kbps
    for orbital0 in {11..12}; do
        for id0 in {01..11}; do
            srcip="192.168.${orbital0}.${id0#0}"
            node="s${orbital0}${id0}"

            for orbital in {11..12}; do
                for id in {01..11}; do
                    dstip="192.168.${orbital}.${id#0}"
                    dstport="1${orbital}${id}"
                    if [ ${srcip} = ${dstip} ]
                    then
                        continue
                    fi
                    docker exec ${node} bash -c "client --srcip=${srcip} --dstip=${dstip} --dstport=${dstport} -n 0 -s 1500 -b ${bandwidth}  > /dev/null 2>&1 &"
                done
            done

        done
    done

    # kill -9 $(ps ux | grep -i '\client --srcip' | awk '{print $2}')
}
###### functions end ######

# 检查当前所在目录是否为 starnet
curr_path=`pwd`

if [ $curr_path != "/root/starnet" ]
then
    echo "运行路径错误！退出脚本..."
    exit
fi

# 设置拓扑信息，先删除旧的，再添加新的
redis-cli -h 192.168.1.183 -a uEstC318.# DEL all_links
python3 utility/bin/topo_22.py
if [ $? -ne 0 ]; then
    echo "设置拓扑信息出错，退出脚本..."
    exit
fi

## 生成相关脚本
rm -rf ~/scripts \
  && mkdir -p ~/scripts/ovs \
  && mkdir -p ~/scripts/link \
  && cp docker_run utility/bin/arp.sh ~/scripts \
  && rm ~/logfile/link/* \
  && rm ~/logfile/server/* \
  && rm ~/logfile/*
./scripts


# 测试后清理环境
# docker stop s{11..12}{01..11} && docker rm s{11..12}{01..11}


